package com.qfedu.bigdataapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qfedu.bigdataapp.R;
import com.qfedu.bigdataapp.bean.HotVideo;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.List;

public class HotVideoAdapter extends BaseAdapter {
    private Context context;
    private List<HotVideo> hotVideoList;

    public HotVideoAdapter(Context context, List<HotVideo> hotVideoList) {
        this.context = context;
        this.hotVideoList = hotVideoList;
    }

    @Override
    public int getCount() {
        return hotVideoList.size();
    }

    @Override
    public HotVideo getItem(int position) {
        return hotVideoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        //1、得到要展示的条目的数据信息
        HotVideo video = hotVideoList.get(position);
        //2、需要有一个展示 热门视频数据的 布局模板
        if (view == null) {//ListView里面还没有缓存对象，需要自己创建
            System.out.println("geView执行:" + position);
            view = LayoutInflater.from(context).inflate(R.layout.item_hotvideo, viewGroup, false);
        }
        ImageView conver = view.findViewById(R.id.img_conver);
        TextView digg = view.findViewById(R.id.tv_digg);

        //3、把数据设置到布局模板上
        long diggCount = video.getDigg_count();
        if (diggCount < 1000) {//若点赞数 < 10000，则直接展示具体数字
            digg.setText(video.getDigg_count() + "");
        } else if (diggCount >= 10000) {
            //展示 a.b 万 的形式
            Double diggFloat = diggCount / 10000d;
            DecimalFormat df = new DecimalFormat("0.0");
            String result = df.format(diggFloat);
            digg.setText(result + " 万");
        }

        //图片链接：video.getItem_cover();
        //加载网络图片
        Picasso.with(context).load(video.getItem_cover()).into(conver);
        return view;
    }
}
